package com.ruoyi.web.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysItem;
import com.ruoyi.system.service.ISysItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author silencezheng
 * @date 2022-10-29
 */
@RestController
@RequestMapping("/system/item")
public class SysItemController extends BaseController
{
    @Autowired
    private ISysItemService sysItemService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('system:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysItem sysItem)
    {
        startPage();
        List<SysItem> list = sysItemService.selectSysItemList(sysItem);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('system:item:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysItem sysItem)
    {
        List<SysItem> list = sysItemService.selectSysItemList(sysItem);
        ExcelUtil<SysItem> util = new ExcelUtil<SysItem>(SysItem.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysItemService.selectSysItemById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('system:item:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysItem sysItem)
    {
        sysItem.setCreateBy(getUsername());
        return toAjax(sysItemService.insertSysItem(sysItem));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('system:item:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysItem sysItem)
    {
        return toAjax(sysItemService.updateSysItem(sysItem));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('system:item:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysItemService.deleteSysItemByIds(ids));
    }

    /**
     * 购买商品
     */
    @PreAuthorize("@ss.hasPermi('system:item:buy')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/buy/{ids}")
    public AjaxResult buy(@PathVariable Long[] ids)
    {
        return toAjax(sysItemService.buySysItem(ids));
    }
}
